from ipmininet.iptopo import IPTopo
from ipmininet.router.config import RouterConfig, Openr
from ipmininet.ipnet import IPNet
from ipmininet.cli import IPCLI
from ipmininet.link import TCIntf
from mininet.util import custom
from mininet.link import TCLink

HOSTS_PER_ROUTER = 1


class OpenrConfig(RouterConfig):
    """A simple config with only a OpenR daemon"""

    def __init__(self, node, *args, **kwargs):
        defaults = {"enable_v4": True}
        super().__init__(node, daemons=((Openr, defaults),), *args, **kwargs)


class SimpleOpenrNet(IPTopo):
    def build(self, *args, **kwargs):
        loss = 0
        delay = "0ms"
        rwash, ratla, rhous, rlosa, rnew, rchic, rkans, rsalt, rseat = self.addRouters('rwash', 'ratla', 'rhous',
                                                                                       'rlosa', 'rnew', 'rchic',
                                                                                       'rkans', 'rsalt', 'rseat',
                                                                                       config=OpenrConfig,
                                                                                       privateDirs=['/tmp', '/var/log'])
        self.addLink(rwash, ratla, loss=loss, delay=delay)
        self.addLink(rwash, rchic, loss=loss, delay=delay)
        link = self.addLink(rwash, rnew, loss=0, delay=delay)
        self.addLink(rnew, rchic, loss=0, delay=delay)
        self.addLink(ratla, rchic, loss=loss, delay=delay)
        self.addLink(ratla, rhous, loss=loss, delay=delay)
        self.addLink(rchic, rkans, loss=loss, delay=delay)
        self.addLink(rhous, rkans, loss=loss, delay=delay)
        self.addLink(rhous, rlosa, loss=loss, delay=delay)
        self.addLink(rkans, rsalt, loss=loss, delay=delay)
        self.addLink(rlosa, rsalt, loss=loss, delay=delay)
        self.addLink(rlosa, rseat, loss=loss, delay=delay)
        self.addLink(rsalt, rseat, loss=loss, delay=delay)
        self.addLink(rnew, self.addHost('h1'))
        self.addLink(rseat, self.addHost('h2'))
        # for r in (rwash, ratla, rhous, rlosa, rnew, rchic, rkans, rsalt, rseat):
        #     for i in range(HOSTS_PER_ROUTER):
        #         self.addLink(r, self.addHost('h%s%s' % (i, r)),
        #                      params2={'v4_width': 5})
        # r1, r2, r3 = self.addRouters('r1', 'r2', 'r3', config=OpenrConfig, privateDirs=['/tmp', '/var/log'])
        # self.addLink(r1, r2)
        # link = self.addLink(r2, r3, loss=50, delay="1000ms")
        #
        # self.addLink(r1, self.addHost('h0'))
        # self.addLink(r3, self.addHost('h1'))
        super().build(*args, **kwargs)

    def build1(self, *args, **kwargs):
        r1, r2, r3, r4 = self.addRouters('r1', 'r2', 'r3', 'r4', config=OpenrConfig,
                                         privateDirs=['/tmp', '/var/log'])
        self.addLinks((r1, r2), (r2, r3), (r3, r4), (r4, r1))
        # self.addLinks((r1, r2), (r2, r3), (r3, r4))
        for r in (r1, r2, r3, r4):
            for i in range(HOSTS_PER_ROUTER):
                self.addLink(r, self.addHost('h%s%s' % (i, r)),
                             params2={'v4_width': 5})

        super().build(*args, **kwargs)


net = IPNet(topo=SimpleOpenrNet(), autoStaticArp=True)
try:
    net.start()
    IPCLI(net)
finally:
    net.stop()
